﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Public;
using System.Data;

namespace DAL
{
    public class PayTransactionDAL
    {
        DBConnection db = new DBConnection();
        //Hàm kiểm tra xem có phải là khách hàng có thẻ thành viên hay không
        public DataTable CheckCard(string inputedCardID)
        {
            return db.loadData("select cardId from LoyaltyCard where cardId!=0 and cardId='" + inputedCardID + "'");
        }

        public DataTable getCustomer(string inputedCardID)
        {
            return db.loadData("select cusName from LoyaltyCard where cardId!=0 and cardId='" + inputedCardID + "'");
        }

        public DataTable getScore(string inputedCardID)
        {
            return db.loadData("select score from LoyaltyCard where cardId!=0 and cardId='" + inputedCardID + "'");
        }

        public DataTable getProductName(string inputedBarcode)
        {
            return db.loadData("select productName from ProductName pn, Product p where barCode='" + inputedBarcode + "'and pn.proNameId =p.proNameId");
        }

        //ham lay product id do len gridview nhung bi invisible
        public DataTable getProductId(string inputedBarcode)
        {
            return db.loadData("select productId from Product where barCode='" + inputedBarcode + "'");
        }
        public DataTable getUnitPrice(string inputedBarcode)
        {
            return db.loadData("select unitPrice from Product where barCode='" + inputedBarcode + "'");

        }


        public int AddInvoice(Invoice invoice)
        {
            int Nparameter = 4;
            string[] name = new string[Nparameter];
            object[] value = new object[Nparameter];
            name[0] = "@cardId"; value[0] = invoice.CardId;
            name[1] = "@cashierId"; value[1] = invoice.CashierId;
            name[2] = "@paymentDate"; value[2] = invoice.PaymentDate;
            name[3] = "@total"; value[3] = invoice.TotalPrice;
            return db.Update("INSERT INTO Invoice(cardId, cashierId, paymentDate, total) VALUES (@cardId, @cashierId, @paymentDate, @total )",
                                    name, value, Nparameter);

        }

        public int AddPurchasing(Purchasing purchasing)
        {
            int nparameter = 4;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@productId"; value[0] = purchasing.ProductId;
            name[1] = "@numOfProduct"; value[1] = purchasing.NumOfProduct;
            name[2] = "@invoiceId"; value[2] = purchasing.InvoiceId;
            name[3] = "@productTotal"; value[3] = purchasing.ProductTotal;

            return db.Update("insert into purchasing(productId, numOfProduct, invoiceId, productTotal) values (@productId, @numOfProduct, @invoiceId, @productTotal)",
                                    name, value, nparameter);
        }


        public DataTable getMaxInvoiceId(string inputedCashierId)
        {
            return db.loadData("select MAX (invoiceId) from Invoice where cashierId  ='" + inputedCashierId + "'");
        }

        public int UpdateScore(LoyaltyCard loyaltyCard)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@score"; value[0] = loyaltyCard.Score;
            name[1] = "@cardId"; value[1] = loyaltyCard.CardId;
            return db.Update("UPDATE LoyaltyCard SET score = @score WHERE cardId = @cardId", name, value, nparameter);
        }
        public int UpdateQuantity(Product product)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@barCode"; value[0] = product.BarCode;
            name[1] = "@quantity"; value[1] = product.Quantity;
            return db.Update("UPDATE Product SET quantity = quantity - @quantity WHERE barCode = @barCode", name, value, nparameter);
        }
    }
}
